package org.kealinghornets.nxtdroid;

import org.kealinghornets.nxtdroid.NXT.LightSensor;
import org.kealinghornets.nxtdroid.NXT.NXTThread;
import org.kealinghornets.nxtdroid.NXT.TouchSensor;

import java.net.PortUnreachableException;

/**
 * Created by jychuah on 11/3/13.
 */
public class MyNXTThread extends NXTThread {

    int state = 1;
    LightSensor eyeball;

    public MyNXTThread() {
        setNXTThreadName("Bridge Crosser");
    }

    public void run() {
        while (state != -1) {            switch(state) {
                case 1 : do_state_1(); break;
                case 2 : do_state_2(); break;
                case 3 : do_state_3(); break;
                case 4 : do_state_4(); break;
                case 5 : do_state_5(); break;
                case 6 : do_state_6(); break;
                case 7 : do_state_7(); break;
                case 8 : do_state_8(); break;
                case 9 : do_state_9(); break;
                case 10 : do_state_10(); break;
                default : state = -1; break;
            }
        }
    }

    public void do_state_1() {
        runMotor(PORT_A,-100);
        runMotor(PORT_B, 98);

        wait(1400);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        eyeball = getActiveLightSensor(PORT_1);

        log("I'm in state 1");

        if (eyeball.getLightValue() >320) {
            state = 2;
        } else {
            state = 3;
        }

    }

    public void do_state_2() {

        runMotor(PORT_A, -100);
        runMotor(PORT_B, -98);

        wait(3750);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        runMotor(PORT_A, 100);
        runMotor(PORT_B, -98);


        wait(1400);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        // do some stuff
        log("I'm in state 2");

        if (eyeball.getLightValue() > 320) {
            state = 4;
        } else {
            state = 5;
        }

    }

    public void do_state_3() {

        runMotor(PORT_A, 100);
        runMotor(PORT_B, -98);

        wait(3170);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        // do some stuff
        log ("I'm in state 3");


            state = 2;

    }

    public void do_state_4() {

        runMotor(PORT_A, -100);
        runMotor(PORT_B, -98);

        wait(7500);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        runMotor(PORT_A, -100);
        runMotor(PORT_B, 98);

        wait(1585);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        runMotor(PORT_A, -100);
        runMotor(PORT_B, -98);

        wait(3750);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        log ("I'm in state 4");

        reset();
        if (eyeball.getLightValue() <300) {
            state = 7;
        } else {
            state = 6;
        }
    }

    public void do_state_5() {

        runMotor(PORT_A, 100);
        runMotor(PORT_B, -98);

        wait(3170);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        getActiveLightSensor(100);


        // do some stuff
        log("I'm in state 5");

        if (eyeball.getLightValue() >320) {
            state = 4;

        }

        reset();
        state = 4;

    }
    public void do_state_6() {

        runMotor(PORT_A, -100 );
        runMotor(PORT_B, -98 );


        wait(7500);

        stopMotor(PORT_A);
        stopMotor(PORT_B);



        // do some stuff
        log("I'm in state 6");

        if (eyeball.getLightValue() >500 ) {
            state = 8;
        }
        reset();
        state = 8;



    }
    public void do_state_7() {

        runMotor(PORT_A, 100);
        runMotor(PORT_B, -98);

        wait(3170);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        runMotor(PORT_A, -100 );
        runMotor(PORT_B, -98);

        wait(12000);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        // do some stuff
        log ("I'm in state 7");

        state = 6;

    }

    public void do_state_8() {

      runMotor(PORT_B, 98);
      runMotor(PORT_A, -100);

        wait(1585);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

      runMotor(PORT_B, -98);
      runMotor(PORT_A, -100);

        wait(7500);

        stopMotor(PORT_A);
        stopMotor(PORT_B);


        log ("I'm in state 8");
        if (eyeball.getLightValue() > 500) {
            state = 10;
        } else {
            state = 9;
        }

    }
    public void do_state_9() {
        runMotor(PORT_A, 100);
        runMotor(PORT_B, -98);

        wait(3170);

        stopMotor(PORT_A);
        stopMotor(PORT_B);

        runMotor(PORT_A, -100);
        runMotor(PORT_B, -98);

        wait(13500);

        stopMotor(PORT_A);
        stopMotor(PORT_B);
        // do some stuff
        log("I'm in state 8");
        if (eyeball.getLightValue() > 500) {
            state = 10;
        }


        state = 10;


    }
    public void do_state_10() {
    runMotor(PORT_C, 100);

        wait(1100);

        stopMotor(PORT_C);

        // do some stuff
        log ("I'm in state 8");
        if (eyeball.getLightValue() > 500) {
            state = 4;
        }
        reset();
        state = -1;


    }}